package com.camera;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.camerakit.CameraKitView;
import com.cjt2325.cameraview.R;
import com.retrofix.BaseObserver;
import com.retrofix.ObservableLoader;
import com.retrofix.RetrofitFactory;
import com.retrofix.entity.BaseEntity;
import com.retrofix.entity.TestBean;

import java.util.List;

public class CameraActivity extends AppCompatActivity {
    private CameraKitView cameraKitView;
    private ImageView imageView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_camera);

        cameraKitView = (CameraKitView) findViewById(R.id.camera);
        imageView = (ImageView) findViewById(R.id.imageView);

        cameraKitView.setFrameCallback(new CameraKitView.FrameCallback() {
            @Override
            public void onFrame(CameraKitView cameraKitView, byte[] bytes) {

            }
        });

        cameraKitView.setCameraListener(new CameraKitView.CameraListener() {
            @Override
            public void onOpened() {

            }

            @Override
            public void onClosed() {

            }
        });

        findViewById(R.id.btn_capture).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cameraKitView.captureImage(new CameraKitView.ImageCallback() {
                    @Override
                    public void onImage(CameraKitView cameraKitView, byte[] bytes) {
                        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        Log.i("111", "bitmap size: width: " + bitmap.getWidth() + ";height: " +bitmap.getHeight());
                        imageView.setImageBitmap(bitmap);
                    }
                });
            }
        });


    }

    @Override
    protected void onStart() {
        super.onStart();
        cameraKitView.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraKitView.onResume();
    }

    @Override
    protected void onPause() {
        cameraKitView.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        cameraKitView.onStop();
        super.onStop();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        cameraKitView.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}
